package com.orangelife.mobile.parkingcard.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.curry.android.util.IntentHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.parkingcard.activity.ManageCarActivity;
import com.orangelife.mobile.parkingcard.activity.ParkingSpaceManagementActivity;
import com.orangelife.mobile.parkingcard.adapter.PrivateParkingListViewlAdapter;
import com.orangelife.mobile.parkingcard.bean.ParkingInfomation;
import com.orangelife.mobile.widget.ScrollListView;
import com.umeng.message.proguard.T;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;

/**
 * Created by yyx
 * on 2015/11/17.
 */
public class PrivateFragment extends Fragment implements View.OnClickListener {

    /** 停了几辆车 */
    private TextView tvParked;
    /** 总共可以停几辆车 */
    private TextView tvParkingTotal;
    private ScrollListView slPlateNumber;
    private Button btnManageCar;
    private Button btnManageParkingSpace;
    private View view;
    private EmptyLayout emptyLayout;

    private List<Map<String, Object>> list;
    /** 从服务端过来的所有绑定的车牌号 */
    private List<Map<String, Object>> list2;
    private String ownerID;
    private PrivateParkingListViewlAdapter<T> adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_parking_private,
                    null);
        }
        initView();
        updateUI();
        /*
         * 缓存的rootView需要判断是否已经被加过parent，
		 * 如果有parent需要从parent删除，要不然会发生这个view已经有parent的错误。
		 */
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        ButterKnife.bind(this, view);
        return view;
    }

    /**
     * 初始化
     */
    private void initView() {
        tvParked = (TextView) view.findViewById(R.id.tvParked);
        tvParkingTotal = (TextView) view.findViewById(R.id.tvParkingTotal);
        slPlateNumber = (ScrollListView) view.findViewById(R.id.slPlateNumber);
        btnManageCar = (Button) view.findViewById(R.id.btnManageCar);
        btnManageParkingSpace = (Button) view.findViewById(R.id.btnManageParkingSpace);
        emptyLayout = (EmptyLayout) view.findViewById(R.id.emptyLayout);
    }

    public void updateUI() {
        emptyLayout.dismiss();
        list = ParkingInfomation.getListUsed();
        list2 = ParkingInfomation.getListTotal();
        ownerID = ParkingInfomation.getOwnerID();
        if (list == null || list2 == null) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
            emptyLayout.setErrorMessage("暂无数据");
            return;
        }
        int totalParkingSpaces = ParkingInfomation.getTotalParkingSpaces();
        int usedParkingSpaces = ParkingInfomation.getUsedParkingSpaces();
        tvParked.setText(usedParkingSpaces + "");
        tvParkingTotal.setText(totalParkingSpaces + "");
        btnManageCar.setOnClickListener(this);
        btnManageParkingSpace.setOnClickListener(this);
        setAdapter();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void setAdapter() {
        adapter = new PrivateParkingListViewlAdapter<>(getActivity(), list);
        slPlateNumber.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnManageCar:
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("list2", (Serializable) list2);
                intent.putExtras(bundle);
                intent.setClass(getActivity(), ManageCarActivity.class);
                startActivity(intent);
                break;
            case R.id.btnManageParkingSpace:
                IntentHelper.getInstances().intentWithExtraString(getActivity(), ParkingSpaceManagementActivity.class, "ownerID", ownerID);

        }
    }
}
